package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ILegalAgreementService;
import com.mdd.admin.validate.LegalAgreementCreateValidate;
import com.mdd.admin.validate.LegalAgreementSearchValidate;
import com.mdd.admin.validate.LegalAgreementUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalAgreementDetailVo;
import com.mdd.admin.vo.LegalAgreementListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalAgreement;
import com.mdd.common.mapper.LegalAgreementMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 协议实现类
 * @author LikeAdmin
 */
@Service
public class LegalAgreementServiceImpl implements ILegalAgreementService {
        
    @Resource
    LegalAgreementMapper legalAgreementMapper;

    /**
     * 协议列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalAgreementListedVo>
     */
    @Override
    public PageResult<LegalAgreementListedVo> list(PageValidate pageValidate, LegalAgreementSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LegalAgreement> queryWrapper = new QueryWrapper<>();

        legalAgreementMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:contents:str",
        });

        IPage<LegalAgreement> iPage = legalAgreementMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LegalAgreementListedVo> list = new LinkedList<>();
        for(LegalAgreement item : iPage.getRecords()) {
            LegalAgreementListedVo vo = new LegalAgreementListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 协议详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalAgreement
     */
    @Override
    public LegalAgreementDetailVo detail(Integer id) {
        LegalAgreement model = legalAgreementMapper.selectOne(
                new QueryWrapper<LegalAgreement>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalAgreementDetailVo vo = new LegalAgreementDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 协议新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalAgreementCreateValidate createValidate) {
        LegalAgreement model = new LegalAgreement();
        model.setTitle(createValidate.getTitle());
        model.setContents(createValidate.getContents());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalAgreementMapper.insert(model);
    }

    /**
     * 协议编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalAgreementUpdateValidate updateValidate) {
        LegalAgreement model = legalAgreementMapper.selectOne(
                new QueryWrapper<LegalAgreement>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setTitle(updateValidate.getTitle());
        model.setContents(updateValidate.getContents());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalAgreementMapper.updateById(model);
    }

    /**
     * 协议删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalAgreement model = legalAgreementMapper.selectOne(
                new QueryWrapper<LegalAgreement>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalAgreementMapper.delete(new QueryWrapper<LegalAgreement>().eq("id", id));
    }

}
